﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PM.SortMusicFoldersByType
{
    class Program
    {
        static void Main(string[] args)
        {
            string split = @"Y:\_tosplit";
            string root = @"Y:\new\The Doors";
            //MoveCoversToMainFolder(@"Y:\new\The Doors");
            //RenameFolders();
            //MoveToSplitFolders(root, split);
            RemoveParentesisFromFolder(root);
        }

        public static void MoveCoversToMainFolder(string rootPath)
        {
            DirectoryInfo root = new DirectoryInfo(rootPath);
            //DirectoryInfo output = new DirectoryInfo(@"E:\tosplit");

            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                foreach (DirectoryInfo dir2 in dir.GetDirectories())
                {
                    bool cover = false;
                    if (dir2.Name == "Covers")
                    {
                        cover = true;
                        foreach (FileInfo file in dir2.GetFiles())
                        {
                            File.Copy(file.FullName, dir.FullName + "\\" + file.Name, true);
                        }
                    }
                    if (cover)
                    {
                        Directory.Delete(dir2.FullName, true);    
                    }

                    if (dir2.Name.StartsWith("CD"))
                    {
                        Directory.Move(dir2.FullName, root.FullName + "\\" + dir.Name + " - " + dir2.Name);
                    }
                }
            }
        }

        public static void RenameFolders(string rootPath)
        {
            DirectoryInfo root = new DirectoryInfo(rootPath);

            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                if (!dir.Name.StartsWith("The Doors"))
                {
                    Directory.Move(dir.FullName, root.FullName + "\\The Doors - " + dir.Name);  
                }
            }
        }

        public static void RemoveParentesisFromFolder(string rootPath)
        {
            DirectoryInfo root = new DirectoryInfo(rootPath);

            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                string newName = "";
                bool rename = false; ;
                if (dir.Name.Contains("("))
                {
                    rename = true;
                    newName = dir.Name.Substring(0, dir.Name.IndexOf('(')) 
                        + " " + dir.Name.Substring(dir.Name.IndexOf(')'));
                }

                if (dir.Name.Contains("["))
                {
                    rename = true;
                    newName = dir.Name.Substring(0, dir.Name.IndexOf('['))
                        + " " + dir.Name.Substring(dir.Name.IndexOf(']')+1);
                }

                if (rename)
                {
                    Directory.Move(dir.FullName, root.FullName + "\\" + newName.Trim());
                }
            }
        }

        public static void MoveToSplitFolders(string rootPath, string split)
        {
            DirectoryInfo root = new DirectoryInfo(rootPath);
            DirectoryInfo splitFolder = new DirectoryInfo(split);

            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                int cueFiles = 0;
                int trackFiles = 0;
                foreach (FileInfo file in dir.GetFiles())
                {
                    if (file.Extension.ToLower().EndsWith("cue"))
                    {
                        cueFiles++;
                    }
                    else if (file.Extension.ToLower().EndsWith("flac") 
                        || file.Extension.ToLower().EndsWith("ape") 
                        || file.Extension.ToLower().EndsWith("wv"))
                    {
                        trackFiles++;
                    }
                }

                if (cueFiles == 1 && trackFiles == 1)
                {
                    Directory.Move(dir.FullName, splitFolder.FullName + "\\" + dir.Name);
                }
            }
        }

        #region
        //DirectoryInfo root = new DirectoryInfo(@"Y:\new\The Doors\2_Remasters");
        ////DirectoryInfo output = new DirectoryInfo(@"E:\tosplit");

        //foreach (DirectoryInfo dir in root.GetDirectories())
        //{
        //    string artist = null;
        //    string album = null;

        //    foreach (FileInfo file in dir.GetFiles())
        //    {
        //        if (file.Extension.ToLower().EndsWith("cue"))
        //        {
        //            TextReader r = file.OpenText();
        //            string cue = r.ReadToEnd();

        //            cue = cue.Substring(cue.IndexOf("FILE")).Split('\r')[0];

        //            string filename = cue.Split('"')[1].Split('"')[0];

        //            if (!File.Exists(dir.FullName + "\\" + filename))
        //            {
        //                Console.WriteLine("ups");
        //            }

        //            cue += r.ReadLine();
        //            cue += r.ReadLine();
        //            cue += r.ReadLine();
        //            cue += r.ReadLine();
        //            cue += r.ReadLine();
        //            Console.WriteLine(cue);
        //            Console.WriteLine();
        //            //artist = file.Name.Split('[')[1].Split(']')[0].Trim();
        //            //album = file.Name.Split(']')[1].Split('.')[0].Trim();
        //        }
        //    }

        //    //if (artist != null &&  !Directory.Exists(string.Format("{0}\\{1} - {2}", root.FullName, artist, album)))
        //    //{
        //    //    Directory.Move(dir.FullName, root.FullName + "\\" + artist + " - " + album);
        //    //}
        
        //static void Main(string[] args)
        //{
        //    DirectoryInfo root = new DirectoryInfo(@"E:\My Documents\Musica");
        //    DirectoryInfo mp3Folder = new DirectoryInfo(@"E:\My Documents\Mp3");
        //    DirectoryInfo losslessFolder = new DirectoryInfo(@"E:\My Documents\Lossless");

        //    foreach (DirectoryInfo dir in root.GetDirectories())
        //    {
        //        bool moveToMp3 = false;
        //        if (!dir.Name.StartsWith("#"))
        //        {
        //            foreach (FileInfo file in dir.GetFiles())
        //            {
        //                if (file.Extension.ToLower().EndsWith("mp3"))
        //                {
        //                    moveToMp3 = true;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            continue;
        //        }
        //        try
        //        {
        //            if (moveToMp3)
        //            {
        //                Directory.Move(dir.FullName, mp3Folder.FullName + "\\" + dir.Name);
        //                //dir.MoveTo(mp3Folder.FullName);
        //            }
        //            else
        //            {
        //                Directory.Move(dir.FullName, losslessFolder.FullName + "\\" + dir.Name);
        //                //dir.MoveTo(losslessFolder.FullName + "\\" + dir.Name);
        //            }
        //        }
        //        catch (Exception)
        //        {
        //            throw;
        //        }
        //    }
        //}
        #endregion
    }
}
